import {getUserInfo} from '../../api/member.js';
import Util from "../../utils/util";
import {CACHE_USERINFO} from "../../config";

var app = getApp()
Page({
  data: {
    userInfo: {},
    fields: {
      avatar: {title: "头像", type: "image", icon: "avatar", class: "weui-cell_access"},
      username: {title: "昵称", icon: "username"},
      // t1: {title: "账号绑定", type: "header"},
      // mobile: {title: "手机号码", icon: "mobile"},
    },
  },
  onLoad: function () {
    var that = this
    Util.chekWxLogin().then(res => {
      that.setData({
        userInfo: res.userInfo,
      })
    }).catch(errmsg => {
    }).finally(() => {
      wx.hideLoading()
    })

  },

  onShow: function () {
    this.getUserInfo()
  },

  chooseWxImage: function (e) {
    let that = this,
      userInfo = this.data.userInfo

    console.log(userInfo)

    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      count: 1,
      success: function (res) {
        let tempFilePaths = res.tempFilePaths
        for (let index = 0; index < tempFilePaths.length; index++) {
          wx.uploadFile({
            url: app.globalData.url + '/api/member/upload',
            filePath: tempFilePaths[index],
            name: "fileData",
            formData: {
              'field_key': "avatar",
              'uid': app.globalData.userInfo.id,
            },
            header: {
              "Content-Type": "multipart/form-data",
              "token": wx.getStorageSync("cookie_token")
            },
            success: function (res) {
              var data = res.data ? JSON.parse(res.data) : {};
              if (data.code) {
                userInfo.avatar = app.globalData.userInfo.avatar = data.data.path
                wx.setStorageSync(CACHE_USERINFO, JSON.stringify(app.globalData.userInfo));
                that.setData({userInfo: userInfo})
              } else {
                app.Tips({title: data.data.msg})
              }
            }
          })
        }
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: this.data.images[e.currentTarget.dataset.key], // 当前显示图片的http链接
      urls: this.data.images // 续药预览的图片http链接列表
    })
  },
  /**
   * 获取个人用户信息
   */
  getUserInfo: function () {
    var that = this;
    getUserInfo().then(res => {
      that.setData({
        userInfo: res.data,
      });
    });
  },
})